package Dao;



public class UserDao extends BaseDao {
	public boolean add(String username, String password, String type) {
		boolean ret = false;
		String sql = "insert into user values('"
						+ username + "','" + password + "','" + type + "')";
		if (!exists(username)) {
			System.out.println("USER NOT EXISTS");
			try {
				createConn();
				stm = conn.createStatement();
				stm.executeUpdate(sql);
				closeStm();
				closeConn();
				ret = true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	public String query(String username, String password) {
		String type = "INVALID";
		if (createConn()) {
			String sql = "select type from user where username = '" + username + "'"
					+ "and password = '" + password + "'";
			try {
				stm = conn.createStatement();
				rs = stm.executeQuery(sql);
				if (rs.next()) {
					type = rs.getString(1);
				}
				closeStm();
				closeConn();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return type;
	}
	
	public boolean exists(String username) {
		boolean ret = false;
		if (createConn()) {
			String sql = "select * from user where username = '" + username + "'";
			try {
				stm = conn.createStatement();
				rs = stm.executeQuery(sql);
				if (rs.next()) {
					ret = true;
				}
				closeStm();
				closeConn();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	
}
